package xyc.szk.web.servlet.context;
/*
 * @Description:
 * @Author: 山志凯
 * @Date: 2021-03-17
 * @Time: 09:18
 */

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

@WebServlet("/context01")

public class ContextServlet01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        ServletContext context = this.getServletContext();

        // this.getServletConfig().getServletContext(); //这样也可以获取ServletContext对象
        //System.out.println("context01:" + context);

        PrintWriter pw = resp.getWriter();
        pw.print("参数为：");
        Enumeration<String> names = context.getInitParameterNames();
        while (names.hasMoreElements()) {
            String key = names.nextElement();
            String value = context.getInitParameter(key);
            pw.print(key+"----"+value);
            pw.println("\n");
        }
        //获取ServletContext域中存储的对象值
        pw.println("className"+context.getAttribute("className"));
    }
}
